package com.xysq.adapter;

import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.flyco.animation.BaseAnimatorSet;
import com.flyco.animation.FadeExit.FadeExit;
import com.flyco.animation.FlipEnter.FlipVerticalSwingEnter;
import com.rockcent.action.AppAction;
import com.rockcent.action.CallbackListener;
import com.rockcent.model.CustCouponOrderBO;
import com.rockcent.model.CustomerBO;
import com.rockcent.model.CustomerCouponBO;
import com.xysq.XYApplication;
import com.xysq.fragment.MyFinishOrderListFragment;
import com.xysq.lemon.R;
import com.xysq.util.PropertiesUtil;
import com.xysq.util.UseUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * 描述信息
 *
 * @author keegan
 * @date 15/8/10
 */
public class MyFinishOrderListAdapter extends BaseDataAdapter<CustCouponOrderBO> {

    AppAction appAction;
    int page;
    int type;
    Activity activity;
    MyFinishOrderListFragment fragment;

    BaseAnimatorSet bas_in = new FlipVerticalSwingEnter();
    BaseAnimatorSet bas_out = new FadeExit();

    public MyFinishOrderListAdapter(MyFinishOrderListFragment fragment, FragmentActivity context, AppAction appAction, int page, int type) {
        super(context);
        this.fragment = fragment;
        activity = context;
        this.appAction = appAction;
        this.page = page;
        this.type = type;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.item_list_finish_order, parent, false);
            holder = new ViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        final CustCouponOrderBO couponOrderBO = itemList.get(position);
        holder.titleTxt.setText(couponOrderBO.getCouponName());
        getCoupon(couponOrderBO.getCouponId(), holder);
        return convertView;
    }

    //查询票券信息
    private void getCoupon(int id, final ViewHolder holder) {
        appAction.viewCustomerCoupon(id, new CallbackListener<CustomerCouponBO>() {
            @Override
            public void onSuccess(CustomerCouponBO data) {
                holder.rewardTv.setText("已被打赏" + data.getSaleCount() + "次");
                getCustomer(String.valueOf(data.getCustomerId()), holder);
                holder.priceTxt.setText(data.getIntroduce());
                String logoUrl = data.getLogoUrl();
                if (TextUtils.isEmpty(logoUrl)) {
                    holder.couponImg.setImageResource(R.drawable.def_img_head);
                } else if (logoUrl.startsWith("http://")) {
                    XYApplication.getImageLoader().displayImage(logoUrl, holder.couponImg);
                } else {
                    XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + logoUrl, holder.couponImg);
                }
            }

            @Override
            public void onFailure(String errorCode, String message) {

            }
        });
    }

    //查询发券人信息
    private void getCustomer(String customerId, final ViewHolder holder) {
        appAction.getCustomer("", "", customerId, new CallbackListener<CustomerBO>() {
            @Override
            public void onSuccess(CustomerBO data) {
                if (TextUtils.isEmpty(data.getHeadUrl()) || data.getHeadUrl().equals("null")) {
                    holder.picIv.setImageResource(R.drawable.def_img_head);
                } else{
                    XYApplication.getImageLoader().displayImage(PropertiesUtil.getImageServer() + data.getHeadUrl(), holder.picIv);
                }
                String name = UseUtil.selectName(data.getNickname(), data.getName(), data.getPhone());
                holder.nameTv.setText(name);
                holder.schoolTv.setText(context.getResources().getString(R.string.txt_from) + data.getSchoolName());
            }

            @Override
            public void onFailure(String errorCode, String message) {
            }
        });
    }

    class ViewHolder {
        @InjectView(R.id.img_coupon)
        ImageView couponImg;
        @InjectView(R.id.txt_title)
        TextView titleTxt;
        @InjectView(R.id.txt_price)
        TextView priceTxt;
        @InjectView(R.id.tv_name)
        TextView nameTv;
        @InjectView(R.id.iv_pic)
        ImageView picIv;
        @InjectView(R.id.tv_school)
        TextView schoolTv;
        @InjectView(R.id.tv_reward)
        TextView rewardTv;

        public ViewHolder(View view) {
            ButterKnife.inject(this, view);
        }
    }

}

